package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.TaskAccessoryInfoEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.http.request.TaskAccessoryInfoRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskAccessoryInfoOneContract;
import com.micangcaihang.micang.mvp.task.model.TaskAccessoryInfoOneModel;

import java.util.List;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 附件信息1
 *     version: 1.0
 * </pre>
 */

public class TaskAccessoryInfoOnePresenter extends TaskAccessoryInfoOneContract.Presenter {
    private TaskAccessoryInfoOneModel mModel;
    private String id;
    private String file_type;

    @Override
    public void setV(TaskAccessoryInfoOneContract.View v) {
        super.setV(v);
        mModel = new TaskAccessoryInfoOneModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setFile_type(String file_type) {
        this.file_type = file_type;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        loadAccessoryInfoData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 加载房屋配套数据
     */
    @Override
    public void loadAccessoryInfoData() {
        Observable<ApiResult<List<TaskAccessoryInfoEntity>>> observable = mModel.getAccessoryInfoData(id, file_type);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<TaskAccessoryInfoEntity>>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<List<TaskAccessoryInfoEntity>> result) {
                if (result.data != null) {
                    mView.returnAccessoryInfoData(result.data);
                }
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
                mView.showNetError(e);
            }
        }));
    }

    /**
     * 保存信息
     *
     * @param request
     */
    @Override
    public void saveAccessoryInfoData(TaskAccessoryInfoRequest request) {
        Observable<ApiResult<Void>> observable = mModel.savaAccessoryInfoData(request);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<Void>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<Void> voidApiResult) {
                mView.showTip("保存成功");
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }
        }));
    }
}
